package mil.af.amc.adpetracker.web.helpers;

import mil.af.amc.adpetracker.model.EquipmentItem;
import mil.af.amc.adpetracker.model.ItemModel;
import mil.af.amc.adpetracker.model.User;
import mil.af.amc.adpetracker.model.comparators.EquipmentItemSerialComparator;
import mil.af.amc.adpetracker.service.*;
import mil.af.amc.radtaglibrary.tags.PageContextAwareBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.PageContext;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * View Helper for EquipmentItem objects
 *
 * @author Jason Ferguson
 */
@Service("equipmentItemViewHelper")
public class EquipmentItemViewHelper implements PageContextAwareBean {

    int userId;

    @Autowired
    public void setUserId(int userId) {
        this.userId = userId;
    }

    private AdpeAccountService adpeAccountService;

    @Autowired
    public void setAdpeAccountService(AdpeAccountService adpeAccountService) {
        this.adpeAccountService = adpeAccountService;
    }

    private EquipmentItemService equipmentItemService;

    @Autowired
    public void setEquipmentItemService(EquipmentItemService equipmentItemService) {
        this.equipmentItemService = equipmentItemService;
    }

    private EquipmentTypeService equipmentTypeService;

    @Autowired
    public void setEquipmentTypeService(EquipmentTypeService equipmentTypeService) {
        this.equipmentTypeService = equipmentTypeService;
    }


    private ItemModelService itemModelService;

    @Autowired
    public void setItemModelService(ItemModelService itemModelService) {
        this.itemModelService = itemModelService;
    }

    private ManufacturerService manufacturerService;

    @Autowired
    public void setManufacturerService(ManufacturerService manufacturerService) {
        this.manufacturerService = manufacturerService;
    }

    private UserService userService;

    @Autowired
    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    private PageContext pageContext;

    @Autowired
    public void setPageContext(PageContext pageContext) {
        this.pageContext = pageContext;
    }

    public List<EquipmentItem> getAll() {

        List<EquipmentItem> itemList = equipmentItemService.getAll();
        Collections.sort(itemList, new EquipmentItemSerialComparator());
        return itemList;

    }

    public String getManufacturer() {

        // TODO: Redo this when I refactor Model

        return "";

    }

    public String getItemModelName() {

        HttpServletRequest httpServletRequest = (HttpServletRequest) pageContext.getRequest();
        int modelId = Integer.parseInt(httpServletRequest.getParameter("modelId"));

        ItemModel model = itemModelService.getById(modelId);
        return model.getName();

    }

    public String getItemModelManufacturerName() {

        HttpServletRequest httpServletRequest = (HttpServletRequest) pageContext.getRequest();
        int modelId = Integer.parseInt(httpServletRequest.getParameter("modelId"));

        ItemModel model = itemModelService.getById(modelId);

        return model.getManufacturer().getName();
    }

    public int getItemModelProcessorSpeed() {

        HttpServletRequest httpServletRequest = (HttpServletRequest) pageContext.getRequest();
        int modelId = Integer.parseInt(httpServletRequest.getParameter("modelId"));

        ItemModel model = itemModelService.getById(modelId);

        return model.getProcessorSpeed();
    }

    public String getModel() {

        // TODO: Redo this when I refactor Model

        return "";
    }

    public int getProcessorSpeed() {

        // TODO: Redo this when I refactor Model

        return 0;
    }

    public List<EquipmentItem> getAllByModel() {

        HttpServletRequest httpServletRequest = (HttpServletRequest) pageContext.getRequest();
        //int adpeAccountId = (Integer) httpServletRequest.getSession().getAttribute("accountId");

        int modelId = Integer.parseInt(httpServletRequest.getParameter("modelId"));
        List<EquipmentItem> itemList = equipmentItemService.getAllByItemModel(modelId);
        Collections.sort(itemList, new EquipmentItemSerialComparator());

        return itemList;
    }

    public List<EquipmentItem> getAllByAccount() {

        HttpServletRequest httpServletRequest = (HttpServletRequest) pageContext.getRequest();
        int adpeAccountId = (Integer) httpServletRequest.getSession().getAttribute("accountId");

        List<EquipmentItem> itemList = equipmentItemService.getAllByAccount(adpeAccountId);
        Collections.sort(itemList, new EquipmentItemSerialComparator());
        return itemList;
    }

    public List<EquipmentItem> getAllCurrentByAccount() {
        HttpServletRequest httpServletRequest = (HttpServletRequest) pageContext.getRequest();
        int adpeAccountId = (Integer) httpServletRequest.getSession().getAttribute("accountId");
        List<EquipmentItem> itemList = equipmentItemService.getAllCurrentByAccount(adpeAccountId);
        Collections.sort(itemList, new EquipmentItemSerialComparator());
        return itemList;
    }

    public List<EquipmentItem> getAllByUser() {

        userId = Integer.parseInt(pageContext.getRequest().getParameter("userId"));

        User user = userService.getUserById(userId);

        List<EquipmentItem> itemList = user.getAssignedEquipment();
        Collections.sort(itemList, new EquipmentItemSerialComparator());
        return itemList;
    }

    public EquipmentItem getItem() {

        int itemId = Integer.parseInt(pageContext.getRequest().getParameter("itemId"));
        EquipmentItem item = equipmentItemService.getById(itemId);

        return item;
    }

    public String getOriginalCost() {

        int itemId = Integer.parseInt(pageContext.getRequest().getParameter("itemId"));
        EquipmentItem item = equipmentItemService.getById(itemId);
        NumberFormat currencyFormatter = NumberFormat.getCurrencyInstance();
        String returnedValue = currencyFormatter.format(item.getOriginalCost());

        return returnedValue;

    }

    public String getDepreciatedValue() {

        int itemId = Integer.parseInt(pageContext.getRequest().getParameter("itemId"));
        EquipmentItem item = equipmentItemService.getById(itemId);

        //return item.getDepreciatedValue();
        NumberFormat currencyFormatter = NumberFormat.getCurrencyInstance();
        String returnedValue = currencyFormatter.format(item.getDepreciatedValue());

        return returnedValue;

    }

    public List<EquipmentItem> getAllByAdpeAccountAndEquipmentTypeAndManufacturer() {

        Integer adpeAccountId = (Integer) pageContext.getRequest().getAttribute("adpeAccountId");
        Integer equipmentTypeId = (Integer) pageContext.getRequest().getAttribute("equipmentTypeId");
        Integer manufacturerId = (Integer) pageContext.getRequest().getAttribute("manufacturerId");

        if (adpeAccountId == null) {
            adpeAccountId = -1;
        }

        if (equipmentTypeId == null) {
            equipmentTypeId = -1;
        }

        if (manufacturerId == null) {
            manufacturerId = -1;
        }
        List<EquipmentItem> results = equipmentItemService.getByAdpeAccountAndEquipmentTypeAndManufacturer(adpeAccountId, equipmentTypeId, manufacturerId);
        Collections.sort(results);

        return results;
    }

    public String getFormattedPurchaseDate() {

        int itemId = Integer.parseInt(pageContext.getRequest().getParameter("itemId"));
        EquipmentItem item = equipmentItemService.getById(itemId);
        Date date = item.getPurchaseDate();

        String formattedDate = new SimpleDateFormat("dd-MMM-yyyy").format(date);

        return formattedDate;
    }

}
